/**
 * Created by wangp on 2016/8/25.
 */
$(function () {
    if (getUserInfo()) {
        /**
         * 个人中心首页
         */
        //加载用户信息
     var   user = getUserInfo();
        console.log(user);

        $('.wode .mui-pull-right.nickname').text(user.user_name);
        $('.wode .mui-pull-right.phone').text(user.phone);
        user.avatar_url && $('.wode .pull-right.user-img > img').attr('src', user.avatar_url);
        $('.img-item-box').on('click', function(){
            $('.file-box input').trigger('click');
        });
        $('.file-box input').on('change', function(){
            var data = new FormData($('form')[0]);
            data.append('userToken', user.userToken);
            $.ajax({
                url: 'http://139.224.70.219:81/imbeer/index.php?s==/User/editUserInfo',
                type: 'POST',
                data: data,
                contentType: false,
                dataType: 'json',
                processData: false,
                async: false,
                cache: false,
                success: function(data){
                    console.log(data);
                    if( typeof data != 'object' ){
                        mui.toast('服务器请求有误，请稍后重试');
                        return false;
                    }
                    if( data.error == 2 ){
                        toLogin();
                    }
                    if (data.error == 0) {
                        user.avatar_id = data.info.download.id;
                        user.avatar_url = data.info.download.url;
                        mui.toast('头像上传成功');
                    } else {
                        mui.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });
    }
});
